﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto.Contact;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteUtil.Attributes;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Web
{
    [Route("api/web/[controller]/[action]")]
    [ApiController]
    [ServiceFilter(typeof(LanguageHeaderFilter))]
    public class ContactController : ControllerBase
    {
        private readonly ContactService _contactService;
        private readonly GlobalConfigService _globalConfigService;
        private readonly LangSplitHelper _langSplitHelper;
        private readonly Mapper _mapper;

        public ContactController(ContactService contactService, GlobalConfigService globalConfigService, LangSplitHelper langSplitHelper, Mapper mapper)
        {
            _contactService = contactService;
            _globalConfigService = globalConfigService;
            _langSplitHelper = langSplitHelper;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取联系方式
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetContactInfo(LangType language)
        {
            try
            {
                var dto = _mapper.MapList<ContactInfoDTO>(await _contactService.GetAllContactsAsync(display:true));
                //多语言翻译
                var lang = _langSplitHelper.ConvertToLang<ContactInfoDTO>(dto, language);
                return new Success(lang);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 右边栏联系方式
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetRightContact(LangType language)
        {
            try
            {
                var list = await _contactService.GetAllContactsAsync(rightDisplay: true);
                var dto = _mapper.MapList<ContactRightDTO>(list);
                var lang = _langSplitHelper.ConvertToLang<ContactRightDTO>(dto, language);
                return new Success(lang);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }
    }
}
